package com.geeksx.trainer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * Copyright (C), 2025, 北京同创永益科技发展有限公司
 *
 * @Classname GlobalCorsConfig
 * @Description TODO
 * @Date 2025/8/27 15:12
 * @Created by zhengkaixuan
 */
@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 允许所有请求
        corsConfiguration.addAllowedOriginPattern("*");
        // 允许携带cookie
        corsConfiguration.setAllowCredentials(true);
        // 允许请求头
        corsConfiguration.addAllowedHeader("*");
        // 允许请求方法
        corsConfiguration.addAllowedMethod("*");
        // 配置路径
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}
